//
//  BuildListDetailViewController.m
//  Tospur-text
//
//  Created by Tospur_hewei on 15/9/25.
//  Copyright (c) 2015年 Tospur_hewei. All rights reserved.
//

#import "BuildListDetailViewController.h"
#import "NetWork.h"
#import "GroundGlass.h"
#import "SearchModel.h"
#import "BuildDetailModel.h"
#import <MBProgressHUD.h>
#import "UITableViewCell+BuildDetailCell.h"
#import <SDCycleScrollView.h>
#import "LineViewController.h"
@interface BuildListDetailViewController ()<SDCycleScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSArray *imageArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UITableViewCell *heigtCell;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic,strong) BuildDetailModel  *buildDetailModel;
@end

@implementation BuildListDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationController.navigationBar.translucent = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667-64) style:UITableViewStylePlain];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RankCell" bundle:nil] forCellReuseIdentifier:@"RankCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ExpertCell" bundle:nil] forCellReuseIdentifier:@"ExpertCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HomeImageCell" bundle:nil] forCellReuseIdentifier:@"HomeImageCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DeveloperCell" bundle:nil] forCellReuseIdentifier:@"DeveloperCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"BuildCell" bundle:nil] forCellReuseIdentifier:@"BuildCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PropertyCell" bundle:nil] forCellReuseIdentifier:@"PropertyCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CommissionCell" bundle:nil] forCellReuseIdentifier:@"CommissionCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"AdviserCell" bundle:nil] forCellReuseIdentifier:@"AdviserCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"AdviserHeaderCell" bundle:nil] forCellReuseIdentifier:@"AdviserHeaderCell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.labelText=@"正在加载...";
    [self requestBuildDetail];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithTitle:@"地图" style:UIBarButtonItemStylePlain target:self action:@selector(rigthAction)];
    self.navigationItem.rightBarButtonItem = barButton;
    
}
- (void)requestBuildDetail{
    NetWork *netWork = [NetWork shareInstance];
    [netWork requestBuildDetailWithGardenId:self.searchModel.GardenId success:^(BuildDetailModel *buildDetailModel) {
        self.buildDetailModel = buildDetailModel;
        [_HUD hide:YES afterDelay:1];
        if([self alertWarning]){
            self.navigationController.navigationBar.translucent = NO;
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
            view = [self cycleScrollView];
            self.tableView.tableHeaderView = view;
            [self.view addSubview:self.tableView];
        }
        else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (BOOL)alertWarning
{
    if ([self.buildDetailModel.status isEqualToString:@"200"]) {
        _HUD.labelText=@"";
        return YES;
    }
    if ([self.buildDetailModel.status isEqualToString:@"405"]) {
        _HUD.labelText=@"无法找到相关信息";
    }
    if ([self.buildDetailModel.status isEqualToString:@"407"]) {
        _HUD.labelText=@"数据库连接异常";
    }
    else
    {
        _HUD.labelText=@"未知错误";
    }
    return NO;
}
- (void)rigthAction
{
    LineViewController *lineVC = [[LineViewController alloc]init];
    [self.navigationController pushViewController:lineVC animated:YES];
}

#pragma mark --- 首页轮播图 ---

- (SDCycleScrollView*)cycleScrollView
{
    //self.imageArray = @[[ UIImage imageNamed:@"方向.jpg"],[ UIImage imageNamed:@"风景.jpg"],[ UIImage imageNamed:@"努力.jpg"],[ UIImage imageNamed:@"坚持.jpg"],[ UIImage imageNamed:@"认真.jpg"]];
    
    //SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, 375, 200) imagesGroup:_imageArray];
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (id object in self.buildDetailModel.GardenImgList) {
        if([[object valueForKey:@"GardenImgType"]isEqualToString:@"0"]){
            [mutableArray addObject:[object valueForKey:@"GardenImgUrl"]];
        }
    }
    NSArray *array = mutableArray;
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, 375, 200) imageURLStringsGroup:array];

    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScrollView.delegate = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // cycleScrollView.titlesGroup = _titleArray;
        cycleScrollView.imageURLStringsGroup = array;
        
        cycleScrollView.contentMode = UIViewContentModeTop;
        cycleScrollView.clipsToBounds = YES;
        
        cycleScrollView.layer.shadowColor = [UIColor blackColor].CGColor;
        cycleScrollView.layer.shadowOpacity = 0.6;
        cycleScrollView.layer.shadowRadius = 1.0;
        cycleScrollView.clipsToBounds = NO;
    });
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 170, 375, 30)];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.text = self.buildDetailModel.GardenName;
    [cycleScrollView addSubview:nameLabel];
    return cycleScrollView;
}
//点击图片的触发方法
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    switch (indexPath.section) {
        case 0:
            cell = [tableView dequeueReusableCellWithIdentifier:@"RankCell"];
            break;
        case 1:
            cell = [tableView dequeueReusableCellWithIdentifier:@"ExpertCell"];
            break;
        case 2:
            cell = [tableView dequeueReusableCellWithIdentifier:@"HomeImageCell"];
            break;
        case 3:
            cell = [tableView dequeueReusableCellWithIdentifier:@"DeveloperCell"];
            break;
        case 4:
            cell = [tableView dequeueReusableCellWithIdentifier:@"BuildCell"];
            break;
        case 5:
            cell = [tableView dequeueReusableCellWithIdentifier:@"PropertyCell"];
            break;
        case 6:
            cell = [tableView dequeueReusableCellWithIdentifier:@"CommissionCell"];
            break;
        case 7:{
            if(indexPath.row > 0)
                cell = [tableView dequeueReusableCellWithIdentifier:@"AdviserCell"];
            else
                cell = [tableView dequeueReusableCellWithIdentifier:@"AdviserHeaderCell"];
            break;
        }
        default:
            break;
    }
    [cell setCellWithModel:self.buildDetailModel];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section < 7)
        return 1;
    else
        return 4;
        //return adviserCount+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"RankCell"];
            break;
        case 1:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"ExpertCell"];
            break;
        case 2:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"HomeImageCell"];
            break;
        case 3:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"DeveloperCell"];
            break;
        case 4:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"BuildCell"];
            break;
        case 5:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"PropertyCell"];
            break;
        case 6:
            self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"CommissionCell"];
            break;
        case 7:{
            if(indexPath.row > 0)
                self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"AdviserCell"];
            else
                self.heigtCell = [tableView dequeueReusableCellWithIdentifier:@"AdviserHeaderCell"];
            break;
        }
        default:
            break;
    }
    CGSize size = [self.heigtCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    NSLog(@"%ld",indexPath.section);
    return size.height;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
